goog.provide('tipfy.tests');

goog.require('goog.dom');
goog.require('goog.dom.forms');
goog.require('goog.dom.query');
goog.require('goog.events');
goog.require('goog.ui.TabBar');

tipfy.tests.init_test_question = function() {
    foo = function(e) {
        var el = e.target;
        var val = goog.dom.forms.getValue(el);
    };

    //goog.events.listen(goog.dom.getElement('genre'), goog.events.EventType.CHANGE, foo);

    // Show-hide explanation field
    var expl = goog.dom.getElement('explanation-field');
    goog.style.setStyle(expl, 'display', 'none');

    var toggle_explanation = function() {
        var status = goog.style.getStyle(expl, 'display'),
        new_status = status == 'none' ? 'block' : 'none';
        goog.style.setStyle(expl, 'display', new_status);
    };

    goog.events.listen(goog.dom.getElement('explanation-switch'), goog.events.EventType.CLICK, toggle_explanation);
};

tipfy.tests.init = function(question_type) {
    tipfy.tests.init_test_question();

    var tab_bar = new goog.ui.TabBar(),
        tab_div = goog.dom.getElement('test-questions-tabs'),
        tabs = goog.dom.query('.goog-tab', tab_div),
        tabs_count = tabs.length,
        genre_field = goog.dom.getElement('genre'),
        question_forms = goog.dom.query('#test-questions-content > div'),
        selected_index = 0,
        selected_id = 'question-' + question_type;

    tab_bar.decorate(tab_div);

    goog.events.listen(tab_bar, goog.ui.Component.EventType.SELECT,
        function(e) {
            var selected_id = e.target.getId() + '-content';

            goog.array.map(question_forms, function(el) {
                goog.style.setStyle(el, 'display', el.id == selected_id ? 'block': 'none');
            });

            goog.dom.forms.setValue(genre_field, e.target.getId().substr(9));
        }
    );

    if(question_type) {
        for (var i = 0; i < tabs_count; i++) {
            if(tabs[i].id == selected_id) {
                selected_index = i;
                break;
            }
        }
    }

    tab_bar.setSelectedTabIndex(selected_index);
};
